package cn.wolfcode.wolf2w.service.impl;


import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.domain.StrategyTheme;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.repository.DestinationRepository;
import cn.wolfcode.wolf2w.repository.StrategyRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import jdk.nashorn.internal.ir.IfNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class StrategyServiceImpl implements IStrategyService {


    //在这里引用后期后台项目引用会导致报错
   /* @Value("${sms.url}")
    private String smsUrl;
    @Value("${sms.Key")
    private String appKey;*/

    @Autowired
    private StrategyRepository repository;
    @Autowired
    private MongoTemplate template;
    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private IStrategyCatalogService strategyCatalogService;

    @Autowired
    private IStrategyThemeService strategyThemeService;
    @Override
    public void save(Strategy strategy) {
        repository.save(strategy);
    }



    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }



    @Override
    public Strategy get(String id) {
        Strategy strategy = repository.findById(id).get();
        return strategy ;
    }
    @Override
    public void update(Strategy strategy){
        if (StringUtils.hasLength(strategy.getId())) {
            Strategy strategy1 = this.get(strategy.getId());
            strategy1.setIsabroad(strategy.getIsabroad());
            if (StringUtils.hasLength(strategy.getCatalogName())){
                strategy1.setCatalogName(strategy.getCatalogName());
            }
            if (StringUtils.hasLength(strategy.getDestId())){
                strategy1.setDestId(strategy.getDestId());
            }
            if (StringUtils.hasLength(strategy.getCatalogId())){
                strategy1.setCatalogId(strategy.getCatalogId());
            }
            if (StringUtils.hasLength(strategy.getContent())){
                strategy1.setContent(strategy.getContent());
            }
            if (StringUtils.hasLength(strategy.getCoverUrl())){
                strategy1.setCoverUrl(strategy.getCoverUrl());
            }
            if (StringUtils.hasLength(strategy.getDestName())){
                strategy1.setDestName(strategy.getDestName());
            }
            strategy1.setFavornum(strategy.getFavornum());
            strategy1.setReplynum(strategy.getReplynum());
            strategy1.setSharenum(strategy.getSharenum());
            if (StringUtils.hasLength(strategy.getSubTitle())){
                strategy1.setSubTitle(strategy.getSubTitle());
            }
            if (StringUtils.hasLength(strategy.getSummary())){
                strategy1.setSummary(strategy.getSummary());
            }
            if (StringUtils.hasLength(strategy.getThemeId())){
                strategy1.setThemeId(strategy.getThemeId());
            }
            if (StringUtils.hasLength(strategy.getThemeName())){
                strategy1.setThemeName(strategy.getThemeName());
            }
            if (StringUtils.hasLength(strategy.getTitle())){
                strategy1.setTitle(strategy.getTitle());
            }
            strategy1.setThumbsupnum(strategy.getThumbsupnum());
            strategy1.setViewnum(strategy.getViewnum());
            this.save(strategy1);
        }
    }
    @Override
    public List<Strategy> list() {
        List<Strategy> strategys = repository.findAll();
        return strategys;
    }

    //分页查询
    @Override
    public Page<Strategy> query(StrategyQuery qo) {
        Query query=new Query();
        //repository只能针对所有数据进行分页
        //template不能直接对数据进行分页
        //使用template间接分页
        //pageResult 核心字段
        //totalCount 总记录数
        if (StringUtils.hasLength(qo.getDestId())){
            query.addCriteria(Criteria.where("destId").is(qo.getDestId()));
        }
        if (StringUtils.hasLength(qo.getThemeId())){
            query.addCriteria(Criteria.where("themeId").is(qo.getThemeId()));
        }
        long totalcount = template.count(query, Strategy.class);
        if (totalcount==0){
            //空的分页对象
            return Page.empty();
        }
        //list 当前页显示数据集合
        Pageable pageable= PageRequest.of(qo.getCurrentPage()-1,qo.getPageSize(), Sort.Direction.ASC,"_id");
        query.with(pageable);
        List<Strategy> list = template.find(query, Strategy.class);
        return new PageImpl<>(list,pageable,totalcount);
    }

    @Override
    public void saveOrUpdate(Strategy strategy){
        StrategyCatalog catalog=strategyCatalogService.get(strategy.getCatalogId());
        strategy.setDestId(catalog.getDestId());

        strategy.setDestId(catalog.getDestName());

        StrategyTheme theme=strategyThemeService.get(strategy.getThemeId());
        strategy.setCatalogName(catalog.getName());

        List<Destination> list = (List<Destination>) destinationService.queryToasts(catalog.getDestId());
        if (list!=null&&list.size()>0){
            Destination destination=list.get(0);
            if ("中国".equals(destination.getName())){
                strategy.setIsabroad(Strategy.ABROAD_NO);
            }else {
                strategy.setIsabroad(Strategy.ABROAD_YES);
            }
            if (StringUtils.hasLength(strategy.getId())){
                this.update(strategy);
            }else{
                strategy.setCreateTime(new Date());
                this.save(strategy);
            }
        }
    }

    //部分更新
    @Override
    public void changeState(String id, int state) {
        Query query=new Query();
        query.addCriteria(Criteria.where("_id").is(id));
        Update update=new Update();
        update.set("state",state);
        template.updateMulti(query,update,Strategy.class);
    }

    @Override
    public List<Strategy> queryCatalogId(String  id) {
       List<Strategy> list=repository.findByCatalogId(id);
        return list;
    }

    @Override
    public List<Strategy> queryViewnumTop3(String destId) {
        //1.目的地id必须指定
        //2.点击量倒序，筛选前三
        Pageable pageable=PageRequest.of(0,3,Sort.Direction.DESC,"viewnum");
        List<Strategy> list=repository.findByDestId(destId,pageable);
        //3.截取前面三个
        return list;
    }

    @Override
    public List<Strategy> queryByDestName(String name) {
        List<Strategy> strategies=repository.findByDestName(name);
        return strategies;
    }
}
